<?php
    require  "../init.php";

    if(empty($_SESSION['admin'])){
        admin_redirect("请登录",3,ADMIN_ROOT_URL . "login.php");
        die;
    }

    //第一次查询时是查询顶级分类,他的父类id为0,查子类的时候就用传过来的id
    $pid = empty($_GET['pid'])? 0 : (int)($_GET['pid']) ;
    //查询分类sql语句
    $sql = "select id , cname, pid, path, display, create_time,update_time from " . PRE ."category where pid = '$pid'";
    //执行sql
    $result = query($link, $sql);
//    mysqli_close($link);
?>

<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Creative - Bootstrap 3 Responsive Admin Template">
    <meta name="author" content="GeeksLabs">
    <meta name="keyword" content="Creative, Dashboard, Admin, Template, Theme, Bootstrap, Responsive, Retina, Minimal">
    <link rel="icon" href="../../public/admin/img/icons/logo.jpg">

    <title>小米 后台(分类管理)</title>

    <!-- Bootstrap CSS -->
    <link href="../../public/admin/css/bootstrap.min.css" rel="stylesheet">
    <!-- bootstrap theme -->
    <link href="../../public/admin/css/bootstrap-theme.css" rel="stylesheet">
    <!--external css-->
    <!-- font icon -->
    <link href="../../public/admin/css/elegant-icons-style.css" rel="stylesheet" />
    <link href="../../public/admin/css/font-awesome.min.css" rel="stylesheet" />
    <!-- full calendar css-->
    <link href="../../public/admin/assets/fullcalendar/fullcalendar/bootstrap-fullcalendar.css" rel="stylesheet" />
    <link href="../../public/admin/assets/fullcalendar/fullcalendar/fullcalendar.css" rel="stylesheet" />
    <!-- easy pie chart-->
    <link href="../../public/admin/assets/jquery-easy-pie-chart/jquery.easy-pie-chart.css" rel="stylesheet" type="text/css" media="screen"/>
    <!-- owl carousel -->
    <link rel="stylesheet" href="../../public/admin/css/owl.carousel.css" type="text/css">
    <link href="../../public/admin/css/jquery-jvectormap-1.2.2.css" rel="stylesheet">
    <!-- Custom styles -->
    <link rel="stylesheet" href="../../public/admin/css/fullcalendar.css">
    <link href="../../public/admin/css/widgets.css" rel="stylesheet">
    <link href="../../public/admin/css/style.css" rel="stylesheet">
    <link href="../../public/admin/css/style-responsive.css" rel="stylesheet" />
    <link href="../../public/admin/css/xcharts.min.css" rel=" stylesheet">
    <link href="../../public/admin/css/jquery-ui-1.10.4.min.css" rel="stylesheet">
    <link href="../../public/admin/css/index.css" rel="stylesheet">
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 -->
    <!--[if lt IE 9]>
    <script src="../../public/admin/js/html5shiv.js"></script>
    <script src="../../public/admin/js/respond.min.js"></script>
    <script src="../../public/admin/js/lte-ie7.js"></script>
    <![endif]-->
</head>

<body>
<!-- container section start -->
<section id="container" class="">
    <!--引入头部-->
    <?php require '../top.php'?>
    <!--引入左边菜单栏-->
    <?php require '../aside.php'?>

    <!--main content start-->
    <section id="main-content">
        <section class="wrapper">
            <!-- page start-->
            <div class="row">
                <div class="col-lg-12">
                    <section class="panel">
                        <header class="panel-heading">
                            分类列表
                        </header>

                        <table class="table table-striped table-advance table-hover">
                            <tbody>
                                <tr>
                                    <th><i class="icon_profile"></i> ID</th>
                                    <th><i class="icon_profile"></i> 分类名称</th>
                                    <th><i class="icon_mobile"></i> 父类名称</th>
                                    <th><i class="icon_mail_alt"></i> PATH</th>
                                    <th><i class="icon_mail_alt"></i> 是否显示</th>
                                    <th><i class="icon_calendar"></i> 创建时间</th>
                                    <th><i class="icon_calendar"></i> 修改时间</th>
                                    <!--根据子类的pid查找父类的pid-->
                                    <?php
                                        $sql = "select pid from ". PRE . "category where id = $pid limit 1";
                                        $li = getRow($link, $sql);
                                        //获取父类的父id
                                        if($li){
                                            $ppid = $li['pid'];
                                        }else{
                                            $ppid = 0;
                                        }

                                    ?>
                                    <th><i class="icon_cogs"></i> Action &nbsp;&nbsp;&nbsp;&nbsp;<a class="btn btn-primary" href="./index.php?pid=<?php echo $ppid;?>">返回</a></th>
                                </tr>
                                <?php if(empty($result)):?>
                                    <tr><td colspan="7"><h1 class="text-center">暂无数据</h1></td></tr>
                                <?php else:?>
                                    <?php foreach($result as $key => $value):?>
                                        <tr>
                                            <td><?php echo $value['id']?></td>
                                            <td><?php echo $value['cname']?></td>
                                            <!--根据子类的pid查找父类的名称-->
                                            <!--如果是顶级分类就输出0-->
                                            <?php
                                                if($value['pid'] != 0){
                                                    $sql = "select cname from ".PRE ."category where id = " .$value['pid'] ." limit 1";
                                                    $p_name_arr = getRow($link, $sql);
                                                    $p_name = $p_name_arr['cname'];
                                                }else{
                                                    $p_name = 0;
                                                }

                                            ?>
                                            <td><?php echo $p_name?></td>
                                            <td><?php echo $value['path']?></td>

                                            <td> <a href="./action.php?act=display&display=<?php echo $value['display']?>&id=<?php echo $value['id']?>"><h3><?php echo $value['display'] == 0 ?'✘' : '✔';?></h3></a></td>

                                            <td><?php echo $value['create_time']?></td>
                                            <td><?php echo $value['update_time']?></td>

                                            <td>
                                                <div class="btn-group">
                                                    <a class="btn btn-primary" href="./index.php?pid=<?php echo $value['id']?>">查看子类</a>
                                                    <a class="btn btn-info" href="./add.php?pid=<?php echo $value['id']?>&path=<?php echo $value['path']?>">添加子类</a>
                                                    <a class="btn btn-success" href="./edit.php?id=<?php echo $value['id'];?>">编辑分类名</a>
                                                    <a class="btn btn-danger" href="./action.php?act=del&id=<?php echo $value['id'];?>&path=<?php echo $value['path'];?>&pid=<?php echo $value['pid'];?>">删除</a>

                                                </div>
                                            </td>
                                        </tr>
                                    <?php endforeach;?>
                                <?php endif;?>

                            </tbody>
                        </table>

                    </section>
                </div>
            </div>
            <!-- page end-->
        </section>
    </section>
    <!--main content end-->

</section>
<!-- container section start -->

<!-- javascripts -->
<script src="../../public/admin/js/jquery.js"></script>
<script src="../../public/admin/js/jquery-ui-1.10.4.min.js"></script>
<script src="../../public/admin/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="../../public/admin/js/jquery-ui-1.9.2.custom.min.js"></script>
<!-- bootstrap -->
<script src="../../public/admin/js/bootstrap.min.js"></script>
<!-- nice scroll -->
<script src="../../public/admin/js/jquery.scrollTo.min.js"></script>
<script src="../../public/admin/js/jquery.nicescroll.js" type="text/javascript"></script>
<!-- charts scripts -->
<script src="../../public/admin/assets/jquery-knob/js/jquery.knob.js"></script>
<script src="../../public/admin/js/jquery.sparkline.js" type="text/javascript"></script>
<script src="../../public/admin/assets/jquery-easy-pie-chart/jquery.easy-pie-chart.js"></script>
<script src="../../public/admin/js/owl.carousel.js" ></script>
<!-- jQuery full calendar -->
<<script src="../../public/admin/js/fullcalendar.min.js"></script> <!-- Full Google Calendar - Calendar -->
<script src="../../public/admin/assets/fullcalendar/fullcalendar/fullcalendar.js"></script>
<!--script for this page only-->
<script src="../../public/admin/js/calendar-custom.js"></script>
<script src="../../public/admin/js/jquery.rateit.min.js"></script>
<!-- custom select -->
<script src="../../public/admin/js/jquery.customSelect.min.js" ></script>
<script src="../../public/admin/assets/chart-master/Chart.js"></script>

<!--custome script for all page-->
<script src="../../public/admin/js/scripts.js"></script>
<!-- custom script for this page-->
<script src="../../public/admin/js/sparkline-chart.js"></script>
<script src="../../public/admin/js/easy-pie-chart.js"></script>
<script src="../../public/admin/js/jquery-jvectormap-1.2.2.min.js"></script>
<script src="../../public/admin/js/jquery-jvectormap-world-mill-en.js"></script>
<script src="../../public/admin/js/xcharts.min.js"></script>
<script src="../../public/admin/js/jquery.autosize.min.js"></script>
<script src="../../public/admin/js/jquery.placeholder.min.js"></script>
<script src="../../public/admin/js/gdp-data.js"></script>
<script src="../../public/admin/js/morris.min.js"></script>
<script src="../../public/admin/js/sparklines.js"></script>
<script src="../../public/admin/js/charts.js"></script>
<script src="../../public/admin/js/jquery.slimscroll.min.js"></script>
<script>

    //knob
    $(function() {
        $(".knob").knob({
            'draw' : function () {
                $(this.i).val(this.cv + '%')
            }
        })
    });

    //carousel
    $(document).ready(function() {
        $("#owl-slider").owlCarousel({
            navigation : true,
            slideSpeed : 300,
            paginationSpeed : 400,
            singleItem : true

        });
    });

    //custom select box

    $(function(){
        $('select.styled').customSelect();
    });

    /* ---------- Map ---------- */
    $(function(){
        $('#map').vectorMap({
            map: 'world_mill_en',
            series: {
                regions: [{
                    values: gdpData,
                    scale: ['#000', '#000'],
                    normalizeFunction: 'polynomial'
                }]
            },
            backgroundColor: '#eef3f7',
            onLabelShow: function(e, el, code){
                el.html(el.html()+' (GDP - '+gdpData[code]+')');
            }
        });
    });



</script>

</body>
</html>